/**
 * #info
 *
 *@author   lulg
 *@createTime   2018/06/05
 *@updateTime   2018/06/05
 *@description  customfilter  component  自定义过滤
 */
/**
 * #item
 *
 * @type        aty-custom-filter
 * @cn            自定义过滤
 * @stPlugin    false
 * @stContainer true
 * @parents    [aty-table-column]
 */
import config from '../../../../config/config.js'

import Emitter from '../../../../mixins/emitter.js'

export default {
  name: config.prefix + '-custom-filter',
  mixins: [Emitter],
  props: {
    /**
     * #property
     *
     * @name    compareType
     * @cn      比较类型
     * @type    list
     * @options [equals,big-equals,small-equals,big,small,in,like-or,open-open,open-close,close-open,close-close]
     * @bind    mustnot
     * @value   equals
     * @description 比较类型,可选[equals,big-equals,small-equals,big,small,in,like-or,open-open,open-close,close-open,close-close]。
     */
    compareType: {
      validator (value) {
        return Artery.isOneOf(value, ['equals', 'big-equals', 'small-equals', 'big', 'small', 'in', 'like-or', 'open-open', 'open-close', 'close-open', 'close-close'])
      },
      default: 'equals'
    },
    /**
     * #property
     *
     * @name    filterValue
     * @cn      过滤值
     * @type    string,number,array
     * @bind    must
     * @description 过滤值。
     */
    filterValue: {
      type: [Number, String, Array],
      default: null
    },
    /**
     * #property
     *
     * @name    dataType
     * @cn      数据类型
     * @type    list
     * @options [string,number,int,short,date]
     * @bind    mustnot
     * @value   string
     * @description 数据类型,可选[string,number,int,short,date]
     */
    dataType: {
      validator (value) {
        return Artery.isOneOf(value, ['string', 'number', 'int', 'short', 'date'])
      },
      default: 'string'
    }
  },
  data () {
    return {
      currentFilterValue: '',
      filterId: 'custom_' + Date.now() + Math.random(),
      filterOpened: false
    }
  },
  methods: {
    dispatchValue () {
      this.dispatch('aty-table-header', 'column-filter', this)
    },
    setCurrentFilterValue (value) {
      if (Array.isArray(value)) {
        this.currentFilterValue = value.join(';')
      } else {
        this.currentFilterValue = value
      }
    },
    handleIconClick () {
      this.filterOpened = !this.filterOpened
    }
  },
  created () {
    this.setCurrentFilterValue(this.filterValue)
    if (this.currentFilterValue) { // 第一次判断有值的情况执行一遍过滤,fixed -> http://code.thunisoft.com/artery/artery6/issues/36
      this.dispatchValue()
    }
  },
  watch: {
    filterValue (value) {
      this.setCurrentFilterValue(value)
      this.dispatchValue() // 后续监听到变化有没有值都走一遍过滤,fixed -> http://code.thunisoft.com/artery/artery6/issues/70
    }
  },
  mounted () {

  }
}
